﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Collections;


/// <summary>
/// 数据库备份类
/// </summary>
public class DataBackup
{
    
    private string _bakName;
    private DateTime  _bakTime;
    private float _dataSize;

    public string BakName
    {
       set { _bakName = value; }
       get { return _bakName; }
    }
    public DateTime BakTime
    {
        
        get { return _bakTime; }
    }
    public float DataSize
    {
        set { _dataSize = value; }
        get { return _dataSize; }
    }

    public DataBackup()
    {
        DateTime baktime=DateTime.Now;
        _bakTime = DateTime.Now;
        //_bakTime = string.Format("{0:yyyyMMddHHmm}",baktime); 
       
    }
    /// <summary>
    /// 获取数据库文件的大小
    /// </summary>
    /// <param name="strPath">数据库文件路径</param>
    public void  GetDatabaseSize(string strPath)
    {
        FileInfo dbfile = new FileInfo(strPath);
        this.DataSize=(float) dbfile.Length / (1024 * 1024);

    }
    /// <summary>
    /// 插入一条新的备份记录
    /// </summary>
    /// <returns>成功返回真，否则假</returns>
    public bool InsertNewRecord()
    {
        bool seccess = false;
        string strInsert = "insert DATABACKUP(BAKNAME,BAKTIME,DATASIZE) values('" + this.BakName + "','" + this.BakTime + "'," + this.DataSize + ")";
        DBOperation db = new DBOperation();
        seccess = db.ExcuteSql(strInsert);
        return seccess;

    }
    /// <summary>
    /// 备份数据库
    /// </summary>
    /// <param name="name">数据库名称</param>
    /// <param name="strPath">备份路径</param>
    /// <returns>成功返回真，否则假</returns>
    public bool Backup(string name, string strPath)
    {
        bool seccess = false;
        string strBackup = "use master;backup database "+name+" to disk='"+strPath+"'";
        DBOperation db = new DBOperation();
        seccess = db.ExcuteSql(strBackup);
        return seccess;
   }
    /// <summary>
    /// 恢复数据
    /// </summary>
    /// <param name="strPath">恢复路径</param>
    /// <returns>成功返回真，否则假</returns>
    public bool Restore( string strPath)
    {
        bool seccss = false;
        string strRestore = "use master;Alter Database XSSFSYSTEM Set Offline with Rollback immediate; restore database " + "XSSFSYSTEM" + " from disk='" + strPath + "'";
        strRestore = strRestore + ";Alter Database XSSFSYSTEM Set Online with Rollback immediate";
        DBOperation db = new DBOperation();
        seccss = db.ExcuteSql(strRestore);
        return seccss;

    }
    /// <summary>
    /// 读取备份记录
    /// </summary>
    /// <returns>返回一个dataset</returns>
    public static  DataSet ReadBackupRecord()
    {
        string strFindRecord = "select * from DATABACKUP";
        DBOperation db = new DBOperation();
        string tableName="backupTable";
        return db.GetDataSet(strFindRecord, tableName);
    }
    /// <summary>
    /// 删除备份数据库文件
    /// </summary>
    /// <param name="msgID">备份记录ID</param>
    /// <param name="strPath">路径</param>
    /// <returns>成功返回真，否则假</returns>
    public bool   DeleteBackupFile(string fileName,string strPath)
    {
        bool seccess=false;
        string deleteMsg = "delete DATABACKUP where BAKNAME='" + fileName + "'";
        DBOperation db = new DBOperation();
        if(db.ExcuteSql(deleteMsg))
        {
        FileInfo backupFile = new FileInfo(strPath);
        backupFile.Delete();
        seccess = true;
        }
        return seccess;
    }
    /// <summary>
    /// 检查是否已经存在同一分钟的备份
    /// </summary>
    /// <param name="fileName">备份文件名</param>
    /// <returns>存在返回真</returns>

    public bool CheckFileName(string fileName)
    {
        bool isExist = false;
        DBOperation db = new DBOperation();
        string checkfileName = "select AUTOID from DATABACKUP where BAKNAME='" + fileName + "'";
        if(db.ExcuteScalar(checkfileName)!=null)
        {
            isExist = true;
        }
        return isExist;
    }

    

}
